let canRoll = true, //加控制，防止用户点击两次
  num = 1, //用在动画上，让用户在第二次点击的时候可以接着上次转动的角度继续转
  lotteryArrLen = 0; //放奖品的数组的长度
Page({
  data: {
    action: 'add',
    id:0,
    name: '',
    itemArr: [],
    itemArrReal: [],
    defaultItem: ''
  },

  onLoad(opt) {
    var id = opt.id;
    var action = opt.action;
    this.setData({
      action: action,
    })
    if (action == 'update') {
      var zpArr = wx.getStorageSync('zpData');
      for (var i = 0; i < zpArr.length; i ++) {
        if (id == zpArr[i].id) {
          var item = zpArr[i];
          this.setData({
            id: item.id,
            name: item.name,
            itemArrReal:item.itemArrReal
          })
          break;
        }
      }
    }

    console.log(this.data)
    this.setPlateData(); //执行设置转盘表面的文字
    let that = this;
    let aniData = wx.createAnimation({ //创建动画对象
      duration: 2000,
      timingFunction: 'ease'
    });
    this.aniData = aniData; //将动画对象赋值给this的aniData属性
  },
  setPlateData() { //设置奖品数组
    var lottery = this.data.itemArrReal;

    lotteryArrLen = lottery.length; //获取奖品数组的长度，用来判断
    if (lotteryArrLen < 2) { //数组的奖品只有一个，扩展数组的长度到4
      let evenArr = new Array(4); //创建一个数组，方便操作。
      for (let i = 0; i < 4; i++) {
        if (i % 2 == 1) { //这里为什么要取1是为了在默认的界面将指针放在谢谢的地方，防止别人拿着中奖的截图来要奖品
          evenArr[i] = lottery[0]; //将原数组的内容赋值到新的数组
        } else {
          evenArr[i] = '没中' //在数组中间隔插入谢谢
        }
      }
      lottery = [...evenArr]; //将整合好的数组赋值给lottery数组
    } else { //数组中的奖品超过1个，则正常扩展数组，扩展的数组为原来的2倍
      let evenArr = new Array(lotteryArrLen * 2); //创建扩展数组
      for (let i = 0; i < (lotteryArrLen * 2); i++) {
        evenArr[i] = lottery[i % lotteryArrLen]; //将原来数组的值赋值给新数组
      }
      lottery = [...evenArr]; //将整合好的数组赋值给lottery数组
    }
    lotteryArrLen = lottery.length; //获取新的数组长度
    this.setData({
      itemArr: lottery //设置好值，用于页面展示
    })
  },
  startRollTap() { //开始转盘
    var lottery = this.data.itemArr;
    let that = this;
    if (canRoll) {
      canRoll = false;
      let aniData = this.aniData; //获取this对象上的动画对象
      let rightNum = ~~(Math.random() * lotteryArrLen); //生成随机数
      console.log(`随机数是${rightNum}`);
      console.log(`奖品是：${lottery[rightNum]}`);
      aniData.rotate(3600 * num - 360 / lotteryArrLen * rightNum).step(); //设置转动的圈数
      this.setData({
        aniData: aniData.export()
      })
      num++;
      canRoll = true;
    }
  },
  bindName: function (e) {
    this.setData({
      name: e.detail.value
    })
  },
  addItem: function (e) {
    var itemArr = this.data.itemArrReal;
    var item = e.detail.value;
    if (item) {
      itemArr.push(item)
      this.setData({
        defaultItem: '',
        itemArrReal: itemArr,
      })
      this.setPlateData();
    }
  },
  updateItem: function (e) {
    var itemIndex = e.currentTarget.dataset.itemIndex;
    var curItem = e.detail.value;
    if (curItem) {
      var itemArr = this.data.itemArrReal;
      var oldItem = itemArr[itemIndex];
      if (oldItem != curItem) {
        itemArr[itemIndex] = curItem;
        this.setData({
          itemArrReal: itemArr,
        });
      }
    } else {
      var itemArr = this.data.itemArrReal;
      if (itemArr.length == 1) {
        this.setData({
          itemArrReal: [],
          itemArr: []
        })
        return;
      } else {
        itemArr.splice(itemIndex, 1);
        this.setData({
          itemArrReal: itemArr,
        })
      }

    }
    this.setPlateData();
  },
  submitItem: function (e) {
    if (!this.data.name) {
      wx.showToast({
        title: '请填写标题',
        icon: 'none',
      })
      return;
    }
    var zpArr = wx.getStorageSync('zpData');
    if (!zpArr) {
      zpArr = [];
    }
    var zpLength = zpArr.length;
    if (this.data.action == 'add') {
      //添加,id 1开始
      var item = {
        id: zpLength + 1,
        name: this.data.name,
        itemArrReal: this.data.itemArrReal
      };
      zpArr.unshift(item);
    } else {
      //更新
      for (var i = 0; i < zpLength; i++) {
        if (this.data.id == zpArr[i].id) {
          var item = zpArr[i];
          item['name'] = this.data.name;
          item['itemArrReal'] = this.data.itemArrReal;
        }
      }
    }
    wx.setStorageSync('zpData', zpArr);
    wx.navigateBack({
      delta: 1,
    })
  }
})
